//
//  ShoppingCartCell.m
//  YunGou
//
//  Created by macbook on 16/5/23.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "ShoppingCartCell.h"
#import <UIImageView+WebCache.h>
#import "NSObject+Tools.h"

@interface ShoppingCartCell()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *numText;
@property (weak, nonatomic) IBOutlet UIImageView *goodsImg;
@property (weak, nonatomic) IBOutlet UILabel *goodsName;
@property (weak, nonatomic) IBOutlet UILabel *surplusNum;

@end

@implementation ShoppingCartCell

- (void)awakeFromNib {
    // Initialization code
    _numText.delegate = self;
    [_numText addTarget:self  action:@selector(valueChanged:)  forControlEvents:UIControlEventAllEditingEvents];
    
}

- (void)valueChanged:(UITextField *)filed{
    if (filed.text.length == 0) {
        filed.text = @"1";
    }
}

- (BOOL)isInputRuleNotBlank:(NSString *)str {
    NSString *pattern = @"^[1-9]\\d*$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:str];
    return isMatch;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if ([self isInputRuleNotBlank:string] || [string isEqualToString:@""]) {//当输入符合规则和退格键时允许改变输入框
        return YES;
    } else {
        return NO;
    }
}


- (void)setProductModel:(ProductModel *)productModel{
    _productModel = productModel;
    _numText.text = [NSString stringWithFormat:@"%ld",productModel.goods_num];
    if (productModel.thumb) {
        [_goodsImg sd_setImageWithURL:[NSURL URLWithString:[ImageUrl stringByAppendingString:productModel.thumb]]];
    }
    _goodsName.text = productModel.title;
    _surplusNum.text = [NSString stringWithFormat:@"%ld",_productModel.zongrenshu-_productModel.canyurenshu];
    
    UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    _goodsImg.userInteractionEnabled = YES;
    [_goodsImg setAccessoryObject:productModel];
    [_goodsImg addGestureRecognizer:imageTap];
}

- (void)handleTap:(UITapGestureRecognizer *)tap{
    UIView *tapView = tap.view;
    ProductModel *productModel = [tapView getAccessoryObject];
    if (self.block) {
        self.block(productModel);
    }
}

- (IBAction)shopCartBtnClicked:(UIButton *)sender {
    [_numText resignFirstResponder];
    NSString *numStr = _numText.text;
    NSInteger num = [numStr integerValue];
    NSInteger oldNum = num;
    if (sender.tag == 1) {
        if (num == 1) {
            return;
        }else{
            _numText.text = [NSString stringWithFormat:@"%ld",--num];
        }
    }else if(sender.tag == 2){
        _numText.text = [NSString stringWithFormat:@"%ld",++num];
    }else{
        _numText.text = [NSString stringWithFormat:@"%ld",num+=10];
    }
    
    NSInteger newNum = [_numText.text integerValue];
    NSInteger subtractNum = newNum - oldNum;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeShopCarNum" object:nil userInfo:@{@"subtractNum":@(subtractNum)}];
    
    [NetWorkTool updateShopCartWithGoodsId:_productModel.Id goodsNum:_numText.text success:^(id object) {
        _productModel.goods_num = newNum;
    } failure:^(NSError *error) {
        
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSInteger subtractNum = [textField.text integerValue] - _productModel.goods_num;
    _productModel.goods_num = [textField.text integerValue];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeShopCarNum" object:nil userInfo:@{@"subtractNum":@(subtractNum)}];
    [NetWorkTool updateShopCartWithGoodsId:_productModel.Id goodsNum:textField.text success:^(id object) {
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
